package util

import (
	"html/template"
	"strconv"
	"strings"
)

//分页对象封装
type Page struct {
	//记录总数
	Count int `json:"_pcount"`
	//当前页码
	CurrentPage int `from:"_cpage" json:"_cpage"`
	//分页数量
	Num int `json:"_num"`
	//每页大小
	PageSize int    `form:"_psize" json:"_psize"`
	Desc     string `form:"_pdesc" json:"_pdesc"`
	Asc      string `form:"_pasc" json:"_pasc"`
	PageHtml template.HTML
	//查询条件拼接进 分页按钮
	Qstr string
}

func NewPage(oldP *Page) *Page {
	return &Page{Count: 0, PageSize: oldP.PageSize, CurrentPage: oldP.CurrentPage, Qstr: oldP.Qstr}
}

func (p *Page) GetPageSize() int {
	if p.PageSize <= 0 {
		p.PageSize = 20
	}
	return p.PageSize
}

func (p *Page) GetCount() int {
	return p.Count
}

func (p *Page) GetCurrentPage() int {
	return p.CurrentPage
}

func (p *Page) GetDesc() string {
	return p.Desc
}

func (p *Page) GetAsc() string {
	return p.Asc
}

//设置总数
func (p *Page) SetCount(count int, customPSize int) {
	if customPSize > 0 {
		p.PageSize = customPSize
	}
	if count < 0 {
		p.Count = 0
	} else {
		p.Count = count
	}

	n := p.Count / p.PageSize
	if p.Count%p.PageSize > 0 {
		n = n + 1
	}
	if n == 0 {
		n = 1
	}
	p.Num = n
	p.PageHtml = p.getPageHTML()
}

//获取分页查询参数
func (p *Page) GetPageParam() (limit int, offset int) {
	return p.GetPageSize(), (p.GetCurrentPage() - 1) * p.GetPageSize()
}

// 拼装页面分页按钮代码
func (p *Page) getPageHTML() template.HTML {
	var htmlStr = ""
	var butHTML = ""
	var nextHTML = ""
	var prevHTML = ""
	var countHTML = "<span class=\"layui-laypage-count\">共 " + strconv.Itoa(p.Count) + " 条</span>"
	var pSizeStr = strconv.Itoa(p.PageSize)

	//生成 每页按钮链接
	var baseURL = p.Qstr
	if cpidx := strings.Index(p.Qstr, "_cpage"); cpidx > 0 {
		baseURL = string(baseURL[0:cpidx])
	}
	if strings.Index(p.Qstr, "?") < 0 {
		baseURL += "?"
	}
	if strings.Index(p.Qstr, "=") >= 0 {
		baseURL += "&"
	}

	for i := 1; i <= p.Num; i++ {
		iStr := strconv.Itoa(i)
		if i == p.CurrentPage {
			butHTML += "<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>" +
				iStr + "</em></span>"
		} else {
			dataURL := baseURL + "_cpage=" + iStr + "&_psize=" + pSizeStr
			butHTML += "<a href=\"javascript:void(0);\" class=\"pagebut\" data-url=\"" + dataURL + "\" data-page=\"" +
				iStr + "\">" + iStr + "</a>"
		}
	}

	//每页记录数 select
	selectDataURL := baseURL + "_cpage=1&_psize="
	var pSizeSelectHTML = "<span class=\"layui-laypage-limits\"><select id=\"__page_limit_select\" data-url=\"" + selectDataURL + "\" lay-ignore=\"\">"
	pSizeArr := [5]int{10, 20, 30, 40, 50}
	for _, pSize := range pSizeArr {
		tpsStr := strconv.Itoa(pSize) //临时
		if pSize == p.PageSize {
			pSizeSelectHTML += "<option value=\"" + tpsStr + "\" selected>" + tpsStr + " 条/页</option>"
		} else {
			pSizeSelectHTML += "<option value=\"" + tpsStr + "\">" + tpsStr + " 条/页</option>"
		}
	}
	pSizeSelectHTML += "</select></span>"

	//当前页等于第一页 prev不可点
	miCpStr := strconv.Itoa(p.CurrentPage - 1)
	if p.CurrentPage == 1 {
		prevHTML = "<a href=\"javascript:void(0);\" class=\"layui-laypage-prev layui-disabled\" data-url=\"\" data-page=\"" + miCpStr + "\">上一页</a>"
	} else {
		dataURL := baseURL + "_cpage=" + miCpStr + "&_psize=" + pSizeStr
		prevHTML = "<a href=\"javascript:void(0);\" class=\"layui-laypage-prev pagebut\" data-url=\"" + dataURL + "\" data-page=\"" + miCpStr + "\">上一页</a>"
	}
	//当前页等于页码总数 next不可点
	if p.CurrentPage == p.Num {
		nextHTML = "<a href=\"javascript:void(0);\" class=\"layui-laypage-next layui-disabled\" data-url=\"\" data-page=\"" + strconv.Itoa(p.CurrentPage+1) + "\">下一页</a>"
	} else {
		dataURL := baseURL + "_cpage=" + strconv.Itoa(p.CurrentPage+1) + "&_psize=" + pSizeStr
		nextHTML = "<a href=\"javascript:void(0);\" class=\"layui-laypage-next pagebut\" data-url=\"" + dataURL + "\" data-page=\"" + strconv.Itoa(p.CurrentPage+1) + "\">下一页</a>"
	}

	//输入跳转
	jumpDataURL := baseURL + "_cpage=#@#&_psize=" + pSizeStr
	var gotoHTML = "<span class=\"layui-laypage-skip\">到第<input type=\"text\" id=\"__page_ipt\" value=\"" +
		strconv.Itoa(p.CurrentPage) + "\" class=\"layui-input\" currentpage=\"" + strconv.Itoa(p.CurrentPage) + "\" data-url=\"" + jumpDataURL + "\" max=\"" + strconv.Itoa(p.Num) + "\">页<button type=\"button\" id=\"__page_confirm\" class=\"layui-laypage-btn\">确定</button></span>"
	//合拼所有代码
	htmlStr = "<div class=\"layui-box layui-laypage layui-laypage-default\">" +
		countHTML + prevHTML + butHTML + nextHTML + pSizeSelectHTML + gotoHTML + "</div>"
	return template.HTML(htmlStr)
}
